package com.yonyou.pmclouds.billview.rmiimpl.buzi;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.billview.entity.BillViewQueryVO;
import com.yonyou.pmclouds.billview.rmitf.IBillViewBuziService;
import com.yonyou.pmclouds.billview.util.BillViewUtil;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogVO;
import com.yonyou.pmclouds.constructionlog.rmiitf.ConstructionLogQuery;
import com.yonyou.pmclouds.processinspect.entity.ProcedureInspectBodyVO;
import com.yonyou.pmclouds.processinspect.entity.ProcedureInspectVO;
import com.yonyou.pmclouds.processinspect.rmiitf.ProcedureInspectRemoteService;
import com.yonyou.pmclouds.team.entity.TeamEnterpriseVO;
import com.yonyou.pmclouds.team.rmiitf.TeamEnterpriseQuery;
import com.yonyou.pmclouds.team.rmiitf.TeamMemberQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class ProcInspectBuziServiceImpl implements IBillViewBuziService {

    @Autowired
    private ProcedureInspectRemoteService procedureInspectRemoteService;
    @Autowired
    private ConstructionLogQuery constructionLogQuery;
    @Autowired
    private TeamMemberQuery teamMemberQuery;
    @Autowired
    private TeamEnterpriseQuery teamEnterpriseQuery;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public String getBillType() {
        return BillTypeConst.PROCEDUREINSPECT_BILLTYPE;
    }

    @Override
    public PageObject<Object> queryBillList(BillViewQueryVO billViewQueryVO) throws BusinessException {

        PageObject<Object>  procedureInspectVOs = procedureInspectRemoteService.queryByCondition(billViewQueryVO.getPkProject(),
                billViewQueryVO.getCreators(),billViewQueryVO.getStartTime(),billViewQueryVO.getEndTime(),
                billViewQueryVO.getBillStatus(),billViewQueryVO.getPageStart(),billViewQueryVO.getPageSize());

        BillViewUtil.setBillStatus(procedureInspectVOs.getObjects());
        // 设置施工单位
        setPkConstructUnit(procedureInspectVOs);

        return procedureInspectVOs;
    }

    /**
     * 设置施工单位
     *
     * @param procedureInspectVOs
     */
    private void setPkConstructUnit(PageObject<Object> procedureInspectVOs) throws BusinessException {
        if (procedureInspectVOs == null || ArrayUtils.isEmpty(procedureInspectVOs.getObjects()))
            return;

        ProcedureInspectVO[] inspectVOS = (ProcedureInspectVO[]) procedureInspectVOs.getObjects();
        Set<String> projectSet = new HashSet<>();
        for (ProcedureInspectVO vo : inspectVOS) {
            projectSet.add(vo.getPk_project());
        }

        TeamEnterpriseVO[] teamEnterpriseVOS = teamEnterpriseQuery.queryByProjects(projectSet.toArray(new String[0]));
        if (ArrayUtils.isEmpty(teamEnterpriseVOS))
            return;

        // 项目和施工单位的映射
        Map<String, String> projectToConsUnitMap = new HashMap<>();
        for (TeamEnterpriseVO enterpriseVO : teamEnterpriseVOS) {
            if (TeamEnterpriseVO.EnterpriseRoleConst.ROLE_CONSTRUCTOR == enterpriseVO.getEntRole()) {
                projectToConsUnitMap.put(enterpriseVO.getPkProject(), enterpriseVO.getPkEnterprise());
            }
        }

        // 给工序报验的施工单位赋值
        for (ProcedureInspectVO inspectVO : inspectVOS) {
            inspectVO.setPk_constructionunit(projectToConsUnitMap.get(inspectVO.getPk_project()));
        }
    }

    @Override
    public SuperVO queryBillDetail(String pkBill) throws BusinessException {
        ProcedureInspectVO procedureInspectVO = procedureInspectRemoteService.queryPrcInspectDetail(pkBill);

        TeamEnterpriseVO[] teamEnterpriseVOS = teamEnterpriseQuery.queryByProject(procedureInspectVO.getPk_project());
        if(teamEnterpriseVOS != null){
            for(TeamEnterpriseVO teamEnterpriseVO : teamEnterpriseVOS){
                if(TeamEnterpriseVO.EnterpriseRoleConst.ROLE_CONSTRUCTOR == teamEnterpriseVO.getEntRole()){
                    procedureInspectVO.setPk_constructionunit(teamEnterpriseVO.getPkEnterprise());
                }else if(TeamEnterpriseVO.EnterpriseRoleConst.ROLE_SUPERVISOR == teamEnterpriseVO.getEntRole()){
                    procedureInspectVO.setPk_supervisionunit(teamEnterpriseVO.getPkEnterprise());
                }
            }
        }

//        TeamMemberVO[] teamMemberVOs =
//                teamMemberQuery.queryByProAndPkUsers(procedureInspectVO.getPk_project(),new String[]{procedureInspectVO.getCreator()});

//        if(ArrayUtils.isNotEmpty(teamMemberVOs)){
//            String pkEnter = teamMemberVOs[0].getPkEnterprise();
//            procedureInspectVO.setPk_constructionunit(pkEnter);
//        }

        if (procedureInspectVO.getBodyVOs() != null) {
            Set<String> constructionLogPks = new HashSet<>();
            for (ProcedureInspectBodyVO bodyVO : procedureInspectVO.getBodyVOs()) {
                constructionLogPks.add(bodyVO.getPk_construct_record());
            }
            Map<String, Object> params = new HashMap<>();
            params.put("ids", constructionLogPks);
            List<ConstructionLogVO> constructionLogVOS = constructionLogQuery.queryVosByParams(params);
            for (ConstructionLogVO constructionLogVO : constructionLogVOS) {
                constructionLogVO.setStrCreationtime((sdf.format(constructionLogVO.getCreationtime())));
                constructionLogVO.setPk_process_inspect(procedureInspectVO.getPk_process_inspect());
            }
            procedureInspectVO.setConstructionLogVOs(constructionLogVOS.toArray(new ConstructionLogVO[0]));
        }

        BillViewUtil.setBillStatus(new ProcedureInspectVO[]{procedureInspectVO});
        BillViewUtil.setFormatDate(new ProcedureInspectVO[]{procedureInspectVO},new String[]{CommonFieldConst.CREATIONTIME},new String[]{"strCreationtime"});

        return procedureInspectVO;
    }
}
